﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// Counts sub columns for a of a key and a super column in a super column family
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftSubCountQuery<K,SN,N> : AbstractThriftCountQuery<K, N>, SubCountQuery<K, SN, N>
    {
        public ThriftSubCountQuery(Keyspace keyspace, Serializer<K> keySerializer,
            Serializer<SN> superNameExtractor, Serializer<N> nameSerializer) :
            base(keyspace, keySerializer, nameSerializer)
        {
            if (superNameExtractor == null)
            {
                throw new NullReferenceException("SuperNameExtractor cannot be null");
            }
            SuperNameSerializer = superNameExtractor;
        }
        public Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        public SN SuperColumnName
        {
            get;
            set;
        }
        public SubCountQuery<K, SN, N> SetSuperColumn(SN sc)
        {
            SuperColumnName = sc;
            return this;
        }
        public override string ToString() 
        {
            return "SubCountQuery(" + ColumnFamily + "," + Key + "," + SuperColumnName + ")";
        }
        public new SubCountQuery<K, SN, N> SetKey(K key)
        {
            return (SubCountQuery<K,SN,N>) base.SetKey(key);
        }
        public new SubCountQuery<K, SN, N> SetColumnFamily(string cf)
        {
            return (SubCountQuery<K,SN,N>) base.SetColumnFamily(cf);
        }
        public new SubCountQuery<K, SN, N> SetColumnNames(List<N> columnNames)
        {
            return (SubCountQuery<K, SN, N>) base.SetColumnNames(columnNames);
        }
        public new SubCountQuery<K, SN, N> SetRange(N start, N finish, int count)
        {
            return (SubCountQuery<K, SN, N>) base.SetRange(start, finish, count);
        }
        public override QueryResult<int> Execute()
        {
            if (Key == null || ColumnFamily == null || SuperColumnName == null)
            {
                throw new NullReferenceException("Key, ColumnFamily and SuperColumnName cannot be null");
            }
            Func<KeyspaceService, int> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamily);
                columnParent.Super_column = SuperNameSerializer.ToBytes(SuperColumnName);
                int count = ks.GetCount(KeySerializer.ToBytes(Key), columnParent, SlicePredicate.ToThrift());
                return count;
            };
            return new QueryResultImpl<int>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<int>(x)), this);
        }
    }
}
